//
//  ServiceHandleCell.m
//  CarLife
//
//  Created by yu on 2018/1/9.
//  Copyright © 2018年 jpas. All rights reserved.
//

#import "ServiceHandleCell.h"

@implementation ServiceHandleCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    if (@available(iOS 13.0, *)) {
        self.contentView.backgroundColor = UIColor.systemGroupedBackgroundColor;
    } else {
        self.contentView.backgroundColor = kBackGroundLightGrayColor;
    }
    
    NSArray *picImageNamesArr = @[@"违章查询",@"违章高发地",@"附近停车",@"周边搜索",@"加油站",@"汽车维修",];
    
    NSMutableArray *temp = [NSMutableArray new];
    for (int i = 0; i < 6; i++) {
        UIButton *button = [UIButton new];
        [self.contentView addSubview:button];
        
        if (@available(iOS 13.0, *)) {
            button.backgroundColor = UIColor.secondarySystemGroupedBackgroundColor;
            [button setTitleColor:UIColor.labelColor forState:UIControlStateNormal];
        } else {
            button.backgroundColor = UIColor.whiteColor;
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        [button setTitle:picImageNamesArr[i] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:picImageNamesArr[i]] forState:UIControlStateNormal];
        //[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = SYSTEM_FONT(13.5);
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        button.tag = i+1;
        [button addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
        
        // 设置button的图片的约束
        button.imageView.sd_layout
        .widthRatioToView(button, 0.2)
        .topSpaceToView(button, 20)
        .centerXEqualToView(button)
        .heightRatioToView(button, 0.3);
        
        button.titleLabel.sd_layout
        .topSpaceToView(button.imageView, 10)
        .centerXEqualToView(button)
        .bottomSpaceToView(button, 17);
        
        button.sd_layout.autoHeightRatio(0.8);
        [temp addObject:button];
    }
    
    [self.contentView setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:3 verticalMargin:1 horizontalMargin:1 verticalEdgeInset:0 horizontalEdgeInset:0];
    [self setupAutoHeightWithBottomView:temp.lastObject bottomMargin:0];
}

- (void)handleAction:(UIButton *)btn {
    if (self.handleActionBlock) {
        self.handleActionBlock(btn.tag);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
